//
// Copyright (C) 2008 Jackson Harper (jackson@ximian.com)

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using Gaia.WebWidgets;


namespace Grr.Web.WebControls
{
    public class RatingsControl : System.Web.UI.WebControls.WebControl
    {
        private static readonly int MaxRating = 5;
        private static readonly string ScriptUrl = "~/Content/scripts/ratings-control.js";
        private static readonly string FullStarUrl = "~/Content/images/rating-control/star-full.gif";
        private static readonly string EmptyStarUrl = "~/Content/images/rating-control/star-empty.gif";
        private static readonly string ClearStarUrl = "~/Content/images/rating-control/clear.gif";

        private static readonly string ClearButtonId = "rating_clear_button";
        private static readonly string LockFieldId = "lock_field";

        public RatingsControl() : base(HtmlTextWriterTag.Div)
        {
            CssClass = "ratings-control";
        }

        public int Rating
        {
            get
            {
                IGrurrahPage gp = Page as IGrurrahPage;
                if (gp == null)
                    return 0;
                return gp.Rating;
            }
        }

        public long GrurrahId
        {
            get
            {
                IGrurrahPage gp = Page as IGrurrahPage;
                if (gp == null)
                    return -1;
                return gp.Id;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptInclude("ratings-control", ResolveUrl (ScriptUrl));
            base.OnPreRender(e);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            HtmlGenericControl rating_container = new HtmlGenericControl();
            HtmlGenericControl lock_container = new HtmlGenericControl ();
            HiddenField rating_field = new HiddenField();
            HiddenField lock_field = new HiddenField();

            lock_field.ID = LockFieldId;

            rating_container.Attributes["class"] = "rating-avg-container";
            lock_container.Attributes["class"] = "rating-locked-container";

            rating_container.Controls.Add(rating_field);
            lock_container.Controls.Add(lock_field);

            rating_field.Value = Rating.ToString (System.Globalization.CultureInfo.InvariantCulture);
            lock_field.Value = "false";

            Controls.Add(rating_container);
            Controls.Add(lock_container);

            for (int i = 0; i < Rating; i++)
            {
                ImageButton img = CreateStarImage(true, i);
                Controls.Add(img);
            }

            for (int i = Rating; i < MaxRating; i++)
            {
                ImageButton img = CreateStarImage(false, i);
                Controls.Add(img);
            }

            ImageButton clear = new ImageButton();
            clear.ImageUrl = Page.ResolveUrl(ClearStarUrl);
            clear.Visible = false;
            Controls.Add(clear);
        }

        private void SubmitRating(int rating)
        {
            // Check if logged in
            // Y:  Submit rating
            // N:  Display error window
            Control clear = FindControl(ClearButtonId);
            HiddenField locked = (HiddenField)FindControl(LockFieldId);
            if (clear != null)
                clear.Visible = false;

            if (locked != null)
                locked.Value = "false";
        }
        
        private ImageButton CreateStarImage(bool full, int index)
        {
            ImageButton img = new ImageButton();

            if (full)
                img.ImageUrl = Page.ResolveUrl(FullStarUrl);
            else
                img.ImageUrl = Page.ResolveUrl(EmptyStarUrl);

            img.ID = index.ToString(System.Globalization.CultureInfo.InvariantCulture);
            img.AlternateText = String.Concat("Rate this a ", index);
            img.Attributes["onmouseout"] = "ratings_clear ();";
            img.Attributes["onmouseover"] = String.Format("ratings_set ({0});", index + 1);
            img.CssClass = "ratings-control-star";

            img.Click += new ImageClickEventHandler(SubmitRatingHandler);
            return img;
        }

        void SubmitRatingHandler(object sender, ImageClickEventArgs e)
        {
            
            /*
            Control control = sender as Control;
            if (control == null)
                return;

            int rating;
            if (!Int32.TryParse (control.ID, out rating))
                return;

            if (rating < 1 || rating > MaxRating)
                return;

            SubmitRating(rating);
             */
        }
    }
}

